package com.comm.utils.file;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;

/**
 * 文件操作工具类
 */
public class FileUtils {

    /**
     * 读取文件，并输出
     * eg：
     *      1.读取本地图片，直接展示在页面上
     * @param filePath
     * @param response
     * @throws Exception eg：IOException、NullException
     */
    public void readFileAndOutput(String filePath, HttpServletResponse response) throws Exception {
        FileInputStream fis = new FileInputStream(filePath);
        int i = fis.available();
        byte[] buff = new byte[i];
        fis.read(buff);
        fis.close();
        response.setHeader("Content-type","image/jpg");
        OutputStream out = response.getOutputStream();
        out.write(buff);
        out.close();
    }

}
